home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Development / 3DMF parser / 1.0 version / MF3DPC / MFFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-07  |  6.8 KB  |  242 lines  |  [TEXT/dosa]

  1. /*==============================================================================
  2.  *
  3.  *    File:        MFFILE.C
  4.  *
  5.  *    Function:    Miscellaneous metafile routines.
  6.  *
  7.  *    Version:    Metafile:    Version 1.0 3DMF files
  8.  *                Package:    Release #2 of this code
  9.  *
  10.  *    Author(s):    Rick Wong (RWW), Duet Development Corp.
  11.  *                John Kelly (JRK), Duet Development Corp.
  12.  *
  13.  *    Copyright:    (c) 1995 by Apple Computer, Inc., all rights reserved.
  14.  *
  15.  *    Change History (most recent first):
  16.  *        FB8_JRK    Segmentation
  17.  *        Fabio    Changed file name to 8 characters
  18.  *        F3F_RWW    File created.
  19.  *==============================================================================
  20.  */
  21.  
  22. #include "MFFILE.H"
  23.  
  24. #include "MF3D.H"
  25. #include "MFERRORS.H"
  26. #include "MFOBJCTS.H"
  27. #include "MFTYPES.H"
  28. #include "MFASSERT.H"
  29. #include "MFINTOBJ.H"
  30. #include "MFMEMORY.H"
  31. #include "MFPRIMTV.H"
  32. #include "MFTEXTST.H"
  33. #include "MFTEXTUT.H"
  34. #include "MFTEXTWR.H"
  35.  
  36. #if defined(applec) || defined(__MWERKS__) || defined(THINK_C)
  37. #pragma segment __MF3D__
  38. #endif
  39.  
  40. /*==============================================================================
  41.  *    MF3D_TypeObjWrite
  42.  *
  43.  *    If this is a new user-defined type, write a Type object
  44.  *==============================================================================
  45.  */
  46. MF3DErr
  47. MF3D_TypeObjWrite(
  48.     MF3D_FilePtr        inMetafilePtr,
  49.     MF3DVoidObjPtr        inMF3DObjPtr)
  50. {
  51.     MF3DUnknownObjPtr    object;
  52.     MF3DInt32            objType;
  53.     MF3DBoolean            foundYet;
  54.     MF3DErr                result;
  55.  
  56.     object = (MF3DUnknownObjPtr)inMF3DObjPtr;
  57.     MFASSERT(object->objectType == kMF3DObjUnknownType);
  58.  
  59.     MFASSERT(sizeof(objType) == sizeof(object->realObjectType));
  60.     objType = (MF3DInt32)(object->realObjectType);
  61.  
  62.     result = kMF3DNoErr;
  63.  
  64.     if (objType <= kMF3DMinimumTypeSeed)
  65.         result = kMF3DErrIllegalUserObjectType;
  66.  
  67.     /* Is it a user-defined type? */
  68.     if (result == kMF3DNoErr && objType < 0)
  69.     {    MF3DUns32            numTypes;
  70.         MF3D_TypeListPtr    typeListPtr;
  71.  
  72.         typeListPtr = inMetafilePtr->typeTable.types;
  73.         foundYet = kMF3DBooleanFalse;
  74.         for (numTypes = inMetafilePtr->typeTable.nTypes;
  75.                 foundYet == kMF3DBooleanFalse && numTypes > 0 &&
  76.                 result == kMF3DNoErr;
  77.                 --numTypes, ++typeListPtr)
  78.         {    if (objType == typeListPtr->number)
  79.             {    if (MF3D_CmpStrInsensitive(typeListPtr->name,
  80.                         (const char *)object->realObjectName) == 0)
  81.                 {    foundYet = kMF3DBooleanTrue;    /* type already in table */
  82.                 }
  83.                 else
  84.                 {    result = kMF3DErrIllegalUserObjectType;    /* type with    */
  85.                                         /* different name already in table    */
  86.                 }
  87.             }
  88.         }
  89.     }
  90.  
  91.     if (result == kMF3DNoErr && foundYet == kMF3DBooleanFalse)
  92.     {    MF3D_TypeListPtr    tempPtr;
  93.         MF3DUns32            numTypes;
  94.  
  95.         /* Add this type to the table */
  96.         ++inMetafilePtr->typeTable.nTypes;
  97.         numTypes = inMetafilePtr->typeTable.nTypes;
  98.         tempPtr = MF3D_Realloc(inMetafilePtr->typeTable.types,
  99.                 numTypes * sizeof(*inMetafilePtr->typeTable.types));
  100.         if (tempPtr == NULL)
  101.             result = kMF3DErrOutOfMemory;
  102.         else
  103.         {    inMetafilePtr->typeTable.types = tempPtr;
  104.             inMetafilePtr->typeTable.types[numTypes - 1].number = objType;
  105.             inMetafilePtr->typeTable.types[numTypes - 1].name =
  106.                     MF3D_DuplicateCString(object->realObjectName);
  107.  
  108.             if (objType < inMetafilePtr->tocStuff.typeSeed)
  109.                 inMetafilePtr->tocStuff.typeSeed = objType - 1;
  110.         }
  111.  
  112.         /* Output the Type object */
  113.         if (result == kMF3DNoErr)
  114.         {    MF3DVoidObj            fakeObj;
  115.             MF3D_ObjStuffPtr    objStuff;
  116.  
  117.             fakeObj.objectType = kMF3DObjType;
  118.             result = MF3D_BeginWrite(inMetafilePtr, &fakeObj, &objStuff);
  119.  
  120.             if (result == kMF3DNoErr)
  121.                 result = (*objStuff->writer) (inMetafilePtr, inMF3DObjPtr);
  122.             
  123.             if (result == kMF3DNoErr)
  124.                 result = MF3D_EndWrite(inMetafilePtr, &fakeObj);
  125.  
  126.             if (result == kMF3DNoErr)
  127.                 MF3D_WriteNewLine(inMetafilePtr);
  128.         }
  129.     }
  130.  
  131.     return result;
  132. }
  133.  
  134. /*==============================================================================
  135.  *    MF3D_CloseReadBuffer
  136.  *
  137.  *    Verify that ReadBuffer is completely popped
  138.  *==============================================================================
  139.  */
  140. MF3DErr
  141. MF3D_CloseReadBuffer(
  142.     MF3D_FilePtr        inMetafilePtr)
  143. {
  144.     MF3DErr        result;
  145.  
  146.     result = kMF3DNoErr;
  147.  
  148.     if (inMetafilePtr->readBuffer.buf != NULL)
  149.     {    MF3D_Free(inMetafilePtr->readBuffer.buf);
  150.         result = kMF3DErrNotEnoughEndGroups;
  151.  
  152.         while (inMetafilePtr->readBuffer.saveSize != NULL)
  153.         {    MF3D_SaveBufferPtr    sizeStack;
  154.     
  155.             sizeStack = inMetafilePtr->readBuffer.saveSize;
  156.             inMetafilePtr->readBuffer.bufSize = sizeStack->bufSize;
  157.             inMetafilePtr->readBuffer.saveSize = sizeStack->next;
  158.             MF3D_Free(sizeStack);
  159.         }
  160.     }
  161.  
  162.     return result;
  163. }
  164.  
  165. /*==============================================================================
  166.  *    MF3D_BackpatchTOCLocation
  167.  *
  168.  *    Patch the location of the TOC into the metafile header
  169.  *==============================================================================
  170.  */
  171. MF3DErr
  172. MF3D_BackpatchTOCLocation(
  173.     MF3D_FilePtr        inMF3DFilePtr)
  174. {
  175.     MF3DErr        result, seekResult;
  176.  
  177.     result = kMF3DNoErr;
  178.  
  179.     /* Backpatch TOC location only for binary files */
  180.     if (!MF3DIsTextFormat(inMF3DFilePtr->dataFormat))
  181.     {    MF3DBinaryFilePosition    curLocation;
  182.  
  183.         result = MF3DTellPosition(inMF3DFilePtr, &curLocation);
  184.  
  185.         if (result == kMF3DNoErr)
  186.         {    result = MF3DSeekPosition(inMF3DFilePtr,
  187.                     inMF3DFilePtr->tocLocation);
  188.         }
  189.  
  190.         if (result == kMF3DNoErr)
  191.             result = MF3D_Uns64Write(inMF3DFilePtr, curLocation);
  192.  
  193.         seekResult = MF3DSeekPosition(inMF3DFilePtr, curLocation);
  194.         if (result == kMF3DNoErr)
  195.             result = seekResult;
  196.     }
  197.     else
  198.     {    MFASSERT(inMF3DFilePtr->tocStuff.tocLabelName != NULL);
  199.         MF3D_ValidateWriteSize(inMF3DFilePtr, strlen(kMF3D_LabelCharStr " ") +
  200.                 strlen(inMF3DFilePtr->tocStuff.tocLabelName));
  201.         result = MF3D_OutputText(inMF3DFilePtr, "%s" kMF3D_LabelCharStr " ",
  202.                 inMF3DFilePtr->tocStuff.tocLabelName);
  203.         MF3D_WriteNewLine(inMF3DFilePtr);
  204.  
  205.         MF3D_Free(inMF3DFilePtr->tocStuff.tocLabelName);
  206.         inMF3DFilePtr->tocStuff.tocLabelName = NULL;
  207.     }
  208.  
  209.     return result;
  210. }
  211.  
  212. /*==============================================================================
  213.  *    MF3D_DisposeTOCStuff
  214.  *
  215.  *    Dispose Table of Contents stuff
  216.  *==============================================================================
  217.  */
  218. MF3DErr
  219. MF3D_DisposeTOCStuff(
  220.     MF3D_FilePtr        inMetafilePtr)
  221. {
  222.     MF3DUns32                entriesLeft;
  223.     MF3D_TOCReferencePtr    curRefPtr;
  224.  
  225.     entriesLeft = inMetafilePtr->tocStuff.numReferences;
  226.  
  227.     if (MF3DIsTextFormat(inMetafilePtr->dataFormat))
  228.     {    curRefPtr = inMetafilePtr->tocStuff.references;
  229.         MFASSERT(curRefPtr != NULL || entriesLeft == 0);
  230.         for ( ; entriesLeft > 0; --entriesLeft, ++curRefPtr)
  231.             MF3D_Free(curRefPtr->ref.name);
  232.     }
  233.  
  234.     MF3D_Free(inMetafilePtr->tocStuff.references);
  235.     /* If there was no Table of Contents, tocLabelName has not been
  236.      * disposed.
  237.      */
  238.     MF3D_Free(inMetafilePtr->tocStuff.tocLabelName);
  239.  
  240.     return kMF3DNoErr;
  241. }
  242.